<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="修读学年" prop="stuYear">
        <!-- <el-input v-model="queryParams.stuYear" placeholder="请输入修读学年" clearable @keyup.enter.native="handleQuery" /> -->
        <el-select
          v-model="queryParams.stuYear"
          placeholder="请输入修读学年"
          clearable
          @keyup.enter.native="handleQuery"
        >
          <el-option v-for="(item, index) in years" :key="index" :value="item"></el-option>
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="修读学期" prop="stuTerm">
        <el-input v-model="queryParams.stuTerm" placeholder="请输入修读学期" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <!-- <el-form-item label="取得学年" prop="getYear">
        <el-input v-model="queryParams.getYear" placeholder="请输入取得学年" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <!-- <el-form-item label="取得学期" prop="getTerm">
        <el-input v-model="queryParams.getTerm" placeholder="请输入取得学期" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <!-- <el-form-item label="年级" prop="grade">
        <el-input v-model="queryParams.grade" placeholder="请输入年级" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <!-- <el-form-item label="学生编号" prop="stuNum">
        <el-input v-model="queryParams.stuNum" placeholder="请输入学生编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <!-- <el-form-item label="课程代码" prop="courseNumber">
        <el-input v-model="queryParams.courseNumber" placeholder="请输入课程代码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <el-form-item label="课程名称" prop="courseName">
        <!-- <el-input v-model="queryParams.courseName" placeholder="请输入课程名称" clearable @keyup.enter.native="handleQuery" /> -->
        <el-select
          v-model="queryParams.courseName"
          placeholder="请输入课程名称"
          clearable
          @keyup.enter.native="handleQuery"
        >
          <el-option v-for="(item, index) in names" :key="index" :value="item"></el-option>
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="课程学分" prop="courseScore">
        <el-input v-model="queryParams.courseScore" placeholder="请输入课程学分" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <!-- <el-form-item label="成绩" prop="stuScore">
        <el-input v-model="queryParams.stuScore" placeholder="请输入成绩" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <!-- <el-form-item label="学分绩点" prop="score">
        <el-input v-model="queryParams.score" placeholder="请输入学分绩点" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <!-- <el-form-item label="取得方式" prop="getWay">
        <el-input v-model="queryParams.getWay" placeholder="请输入取得方式" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <!-- <el-form-item label="修读性质" prop="readState">
        <el-input v-model="queryParams.readState" placeholder="请输入修读性质" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <!-- <el-form-item label="考核方式" prop="testWay">
        <el-input v-model="queryParams.testWay" placeholder="请输入考核方式" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <!-- <el-form-item label="类型1" prop="courseType1">
        <el-input v-model="queryParams.courseType1" placeholder="请输入课程类型1" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <!-- <el-form-item label="类型2" prop="courseType2">
        <el-input v-model="queryParams.courseType2" placeholder="请输入课程类型2" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <!-- <el-form-item label="备注" prop="remark">
        <el-input v-model="queryParams.remark" placeholder="请输入备注" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPermi="['courseSelect:score:add']">新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['courseSelect:score:edit']">修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
                   v-hasPermi="['courseSelect:score:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                   v-hasPermi="['courseSelect:score:export']">导出</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button type="info" icon="el-icon-upload2" size="mini" @click="handleImport"
                   v-hasPermi="['courseSelect:score:import']">导入</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- <div style="display: flex;">
      <div ref="barChart" style="width: 600px;height:400px;"></div>
      <div ref="pieChart" style="width: 600px;height:400px;"></div>
    </div> -->

    <!-- <div class="container">
      <div ref="barChart" class="chart"></div>
      <div ref="pieChart" class="chart"></div>
    </div> -->

    <el-row :gutter="10">
      <el-col :span="12">
        <el-card>
          <div id="line" class="chart" ></div>
        </el-card>
      </el-col>
      <el-col :span="12">
        <el-card>
          <div id="pie" class="chart"></div>
        </el-card>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="scoreList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="成绩id" align="center" prop="scoreId" /> -->
      <el-table-column label="修读学年" align="center" prop="stuYear" />
      <el-table-column label="修读学期" align="center" prop="stuTerm" />
      <el-table-column label="取得学年" align="center" prop="getYear" />
      <el-table-column label="取得学期" align="center" prop="getTerm" />
      <el-table-column label="年级" align="center" prop="grade" />
      <el-table-column label="学生姓名" align="center" prop="stuNum" />
      <el-table-column label="课程代码" align="center" prop="courseNumber" />
      <el-table-column label="课程名称" align="center" prop="courseName" />
      <el-table-column label="课程学分" align="center" prop="courseScore" />
      <el-table-column label="成绩" align="center" prop="stuScore" />
      <!-- <el-table-column label="学分绩点" align="center" prop="score" /> -->
      <el-table-column label="取得方式" align="center" prop="getWay" />
      <el-table-column label="修读性质" align="center" prop="readState" />
      <!-- <el-table-column label="考核方式" align="center" prop="testWay" /> -->
      <!-- <el-table-column label="课程类型1" align="center" prop="courseType1" /> -->
      <!-- <el-table-column label="课程类型2" align="center" prop="courseType2" /> -->
      <!--      <el-table-column label="备注" align="center" prop="remark" />-->
      <!-- <el-table-column label="0 停用 1 启用" align="center" prop="status" /> -->
      <!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
      <!--        <template slot-scope="scope">-->
      <!--          &lt;!&ndash; <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"-->
      <!--            v-hasPermi="['courseSelect:score:edit']">修改</el-button> &ndash;&gt;-->
      <!--          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"-->
      <!--            v-hasPermi="['courseSelect:score:remove']">删除</el-button>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
                @pagination="getList" />

    <!-- 用户导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px">
      <el-upload ref="upload" :limit="1" accept=".xlsx, .xls" :headers="upload.headers"
                 :action="upload.url + '?updateSupport=' + upload.updateSupport" :disabled="upload.isUploading"
                 :on-progress="handleFileUploadProgress" :on-success="handleFileSuccess" :auto-upload="false" drag>
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">
          将文件拖到此处，或
          <em>点击上传</em>
        </div>
        <div class="el-upload__tip" slot="tip">
          <el-checkbox v-model="upload.updateSupport" />是否更新已经存在的用户数据
          <el-link type="info" style="font-size:12px" @click="importTemplate">下载模板</el-link>
        </div>
        <div class="el-upload__tip" style="color:red" slot="tip">提示：仅允许导入“xls”或“xlsx”格式文件！</div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改课程成绩对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="修读学年" prop="stuYear">
          <el-input v-model="form.stuYear" placeholder="请输入修读学年" />
        </el-form-item>
        <el-form-item label="修读学期" prop="stuTerm">
          <el-input v-model="form.stuTerm" placeholder="请输入修读学期" />
        </el-form-item>
        <el-form-item label="取得学年" prop="getYear">
          <el-input v-model="form.getYear" placeholder="请输入取得学年" />
        </el-form-item>
        <el-form-item label="取得学期" prop="getTerm">
          <el-input v-model="form.getTerm" placeholder="请输入取得学期" />
        </el-form-item>
        <el-form-item label="年级" prop="grade">
          <el-input v-model="form.grade" placeholder="请输入年级" />
        </el-form-item>
        <el-form-item label="学生编号" prop="stuNum">
          <el-input v-model="form.stuNum" placeholder="请输入学生编号" />
        </el-form-item>
        <el-form-item label="课程代码" prop="courseNumber">
          <el-input v-model="form.courseNumber" placeholder="请输入课程代码" />
        </el-form-item>
        <el-form-item label="课程名称" prop="courseName">
          <el-input v-model="form.courseName" placeholder="请输入课程名称" />
        </el-form-item>
        <el-form-item label="课程学分" prop="courseScore">
          <el-input v-model="form.courseScore" placeholder="请输入课程学分" />
        </el-form-item>
        <el-form-item label="成绩" prop="stuScore">
          <el-input v-model="form.stuScore" placeholder="请输入成绩" />
        </el-form-item>
        <el-form-item label="学分绩点" prop="score">
          <el-input v-model="form.score" placeholder="请输入学分绩点" />
        </el-form-item>
        <el-form-item label="取得方式" prop="getWay">
          <el-input v-model="form.getWay" placeholder="请输入取得方式" />
        </el-form-item>
        <el-form-item label="修读性质" prop="readState">
          <el-input v-model="form.readState" placeholder="请输入修读性质" />
        </el-form-item>
        <el-form-item label="考核方式" prop="testWay">
          <el-input v-model="form.testWay" placeholder="请输入考核方式" />
        </el-form-item>
        <el-form-item label="课程类型1" prop="courseType1">
          <el-input v-model="form.courseType1" placeholder="请输入课程类型1" />
        </el-form-item>
        <el-form-item label="课程类型2" prop="courseType2">
          <el-input v-model="form.courseType2" placeholder="请输入课程类型2" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listScore, getScore, delScore, addScore, updateScore, getBarData, getPieData, getScoreNames, getScoreYears } from "@/api/courseSelect/score";
import { getToken } from "@/utils/auth";
import * as echarts from 'echarts';

const barOption = {
  // ECharts 配置项
  title: {
    text: '课程成绩'
  },
  tooltip: {},
  legend: {
    data:['人数占比']
  },
  xAxis: {
    data: ["0 ~ 20","21 ~ 40","41 ~ 60","61 ~ 80","81 ~ 100"]
  },
  yAxis: {

  },
  series: [{
    name: '人数比',
    type: 'bar',
    data: [5, 20, 36, 20, 20]
  }]
};

const pieOption = {
  // 饼图配置
  title: {
    text: '通过情况',
    left: 'center'
  },
  tooltip: {
    trigger: 'item'
  },
  legend: {
    orient: 'vertical',
    left: 'left'
  },
  series: [
    {
      name: '通过情况',
      type: 'pie',
      radius: '50%',
      data: [
        { value: 50, name: '通过' },
        { value: 735, name: '补考' },
        { value: 580, name: '重修' },
      ],
      emphasis: {
        itemStyle: {
          shadowBlur: 10,
          shadowOffsetX: 0,
          shadowColor: 'rgba(0, 0, 0, 0.5)'
        }
      }
    }
  ]
};

export default {
  name: "Score",
  data() {
    return {
      myBarChart: null, // 存储柱状图实例
      myPieChart: null, // 存储饼图实例
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 课程成绩表格数据
      scoreList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        // pageNum: 1,
        // pageSize: 10,
        stuYear: null,
        stuTerm: null,
        getYear: null,
        getTerm: null,
        grade: null,
        stuNum: null,
        courseNumber: null,
        courseName: null,
        courseScore: null,
        stuScore: null,
        score: null,
        getWay: null,
        readState: null,
        testWay: null,
        courseType1: null,
        courseType2: null,
        remark: null,
        status: null,
        pageSize: 10
      },
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/courseSelect/score/importData"
      },
      years:[],
      names:[],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        stuNum: [
          { required: true, message: "学生编号不能为空", trigger: "blur" }
        ],
        courseNumber: [
          { required: true, message: "课程代码不能为空", trigger: "blur" }
        ],
        courseName: [
          { required: true, message: "课程名称不能为空", trigger: "blur" }
        ],
        courseScore: [
          { required: true, message: "课程学分不能为空", trigger: "blur" }
        ],
        stuScore: [
          { required: true, message: "成绩不能为空", trigger: "blur" }
        ],
        score: [
          { required: true, message: "学分绩点不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "0 停用 1 启用不能为空", trigger: "change" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  mounted() {
    // let lineDom = document.getElementById('line');
    // let lineChar = echarts.init(lineDom)
    // barOption.series[0].data[0] = this.scoreList.length;
    // lineChar.setOption(barOption);
    // getBarData().then(res => {
    //   barOption.series[0].data = res
    //   let lineDom = document.getElementById('line');
    //   let lineChar = echarts.init(lineDom)
    //   lineChar.setOption(barOption);
    // })

    // let pieDom = document.getElementById('pie');
    // let pieChar = echarts.init(pieDom)
    // pieChar.setOption(pieOption)
  },
  methods: {
    // initBarChart() {
    //   this.myBarChart = echarts.init(this.$refs.barChart);
    //   // this.barChart
    //   this.myBarChart.setOption(barOption);
    // },
    // initPieChart() {
    //   this.myPieChart = echarts.init(this.$refs.pieChart);
    //   this.myPieChart.setOption(pieOption);
    // },
    /** 查询课程成绩列表 */
    getList() {
      this.loading = true;
      listScore(this.queryParams).then(response => {
        this.scoreList = response.rows;
        this.total = response.total;
        this.loading = false;


        getBarData(this.queryParams).then(res => {
          barOption.series[0].data = res.data;
          let lineDom = document.getElementById('line');
          let lineChar = echarts.init(lineDom)
          lineChar.setOption(barOption);
        });

        getPieData(this.queryParams).then(res => {
          pieOption.series[0].data = res.data;
          let pieDom = document.getElementById('pie');
          let pieChar = echarts.init(pieDom)
          pieChar.setOption(pieOption);
        })

        getScoreYears().then(res => {
          this.years = res.data;
        })

        getScoreNames().then(res => {
          this.names = res.data;
        })
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        scoreId: null,
        stuYear: null,
        stuTerm: null,
        getYear: null,
        getTerm: null,
        grade: null,
        stuNum: null,
        courseNumber: null,
        courseName: null,
        courseScore: null,
        stuScore: null,
        score: null,
        getWay: null,
        readState: null,
        testWay: null,
        courseType1: null,
        courseType2: null,
        remark: null,
        updateTime: null,
        createTime: null,
        status: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.scoreId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加课程成绩";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const scoreId = row.scoreId || this.ids
      getScore(scoreId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改课程成绩";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.scoreId != null) {
            updateScore(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addScore(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const scoreIds = row.scoreId || this.ids;
      this.$modal.confirm('是否确认删除课程成绩编号为"' + scoreIds + '"的数据项？').then(function () {
        return delScore(scoreIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('courseSelect/score/export', {
        ...this.queryParams
      }, `score_${new Date().getTime()}.xlsx`)
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "用户导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('courseSelect/score/importTemplate', {
      }, `score_template_${new Date().getTime()}.xlsx`)
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(response.msg, "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    }
  }
};
</script>

<style>
.container {
  display: flex;
  justify-content: center; /* 水平居中 */
  align-items: center; /* 垂直居中 */
  border: 3px solid #4A90E2; /* 美化边框，可以根据喜好调整颜色和宽度 */
  border-radius: 10px; /* 边框圆角 */
  padding: 20px; /* 内部填充 */
  gap: 20px; /* 元素间距 */
  margin: 20px; /* 外边距 */
  box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1); /* 添加阴影效果 */
}

.chart {
  width: 600px;
  height: 400px;/* 为了可视化，暂时添加背景色 */
}

/* 响应式布局 */
@media (max-width: 1200px) {
  .container {
    flex-direction: column; /* 小屏幕时改为垂直排列 */
  }

  .chart {
    width: 100%; /* 图表宽度自适应 */
  }
}
</style>
